package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: NursingProjectServiceImpl
 * Package: com.zzyl.service.impl
 * Description:
 *
 * @Author 贺亚泽
 * @Create 2024/6/6 下午12:11
 * @Version 1.0
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {

        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> list =  nursingProjectMapper.getByPage();

        return PageResponse.of(list,NursingProjectVo.class);
    }

    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectByPrimaryKey(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {

        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.updateByPrimaryKeySelective(bean);

    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    @Override
    public void delete(Long id) {
        nursingProjectMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProject> nursingProjects = nursingProjectMapper.selectAll();
        List<NursingProjectVo> vos = new ArrayList<NursingProjectVo>();
        for (NursingProject nursingProject : nursingProjects) {
            vos.add(BeanUtil.toBean(nursingProject, NursingProjectVo.class));
        }
        return vos;
    }
}
